package main

import (
	"api/auth"
	"api/common"
	"crypto/tls"
	"fmt"
	"io"
	"log"
	"net/http"
	"strings"
)

func main() {

	url := "https://api.saltyun.com:30888/servers"
	method := "POST"

	// ✅ 创建跳过 TLS 验证的 http.Client
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}

	payload := strings.NewReader(`{
    "count": 1,
    "server": {
		"name": "vm-host1",
		"description": "私人电脑",
        "auto_start": true,
		"network": "hosts-net",
		"vmem_size": 1024,
        "vcpu_count": 2,
		"disable_delete": false,		
		"keypair_id": "386b72c9-95e1-4b6f-8124-361a904363cb",
        "disks": [
            {
				"boot_index": 0,
                "disk_type": "sys",
                "format": "qcow2",
				"size": 30720,
                "image_id": "Ubuntu Server-Linux-22.04(Jammy)-x86_64-current"
            }
        ],
		"prefer_zone": "zone0",
        "hostname": "myhost"
    }
}`)
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
		return
	}

	token, err := auth.TokenPort("sysadmin", "N48XSRmcsP5YMcPf")
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Add("X-Auth-Token", token)
	req.Header.Add("Content-Type", "application/json")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}

	common.OutputJson(string(body))
}
